#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
FABRIC_CA="$(cd "$(dirname "$0")/.." && pwd)"
SCRIPTDIR="$FABRIC_CA/scripts/fvt"
. "$SCRIPTDIR/fabric-ca_utils"
export RESULTLOG="/tmp/fvt-test.results"
export STARTIME=$SECONDS
export PATH="$PATH:$FABRIC_CA/build/tools"
export RC=0
true >$RESULTLOG

function runTests() {
  echo "Running fvt tests ..."
  echo ""

  tests="$(find "$SCRIPTDIR" -maxdepth 1 -name "*test.*sh"| sort)"
  for cmd in $tests; do
    runTest "${cmd}"
  done
}

function runTest() {
  export TESTCASE="${1##*/}"
  echo "*******************" | tee -a $RESULTLOG 2>&1
  echo -n " Running $TESTCASE " | tee -a $RESULTLOG 2>&1
  ${1} >>$RESULTLOG 2>&1
  rc=$?
  test $rc -eq 0 && echo PASSED || echo FAILED
  test $rc -ne 0 && awk -v b="$TESTCASE" -v e="test ended." '$0~b,$0~e' $RESULTLOG
  RC=$((RC + rc))
  "$SCRIPTDIR/fabric-ca_setup.sh" -R >/dev/null 2>&1
}

TimeStamp | tee $RESULTLOG
runTests
grep -a RC: $RESULTLOG
echo "Finished running fvt tests"
TESTCASE="fabric-ca-fvt"
CleanUp "$RC"
exit "$RC"
